﻿// A dialog is a panel whose parent is an overlay
define([
    'Wjs/ui/widgets/Panel',
    'Wjs/ui/widgets/foundation/Overlay',
    'Wjs/ui/widgets/tools/Closer',
    'Wjs/ui/widgets/fields/Button',
    'Wjs/ui/elements/Text'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Dialog',

        internal: {

            base: Wjs.getType('Panel'),

            initialize: function () {
                var me = this,
                    overlay = Wjs.new('Overlay').setItem(me); // Create the parent overlay

                me.addClass('wjs-dialog');

                me.setClosable();

                me.center(true);

                me.onBeforeRender(function () {

                    overlay.render();

                    if (me.overlayRendered) { // The overlay will render the children as well, so the second time it gets called return false

                        return false;
                    }

                    me.overlayRendered = true;
                });
            }

        },

        //setParent: function (parent) {

        //    throw 'Invalid operation: Dialog setParent';
        //},

        setModal: function (modal) {

            this.modal = modal;

            return this;
        },

        setMovable: function (movable) {

            this.movable = movable;

            return this;
        },

        setClosable: function (closable) {

            if (closable === false) {

                throw 'Not implemented';

            }
            else {

                this.getSection('header').getSection('tools')
                    .addItem(

                        Wjs.new('Closer')
                            .setTarget(this)
                    );
            }

            return this;
        },

        center: function (flag) {

            if (flag) {

                this.addClass('wjs-center');
            }
            else {

                this.removeClass('wjs-center');
            }

        },

        addButton: function (button) {

            button = Wjs.isString(button) ? this.createButton(button) : this.createItem(button);

            this.getSection('footer').addItem(button);

            return this;
        },

        onOk: function (fcn) {

            this.okHandler = fcn;

            return this;
        },

        onCancel: function (fcn) {

            this.cancelHandler = fcn;

            return this;
        },

        onBeforeClose: function (fcn) {

            this.beforeCloseHandler = fcn;

            return this;
        },

        onClose: function (fcn) {

            this.closeHandler = fcn;

            return this;
        },

        // Creates the pre-defined buttons from a string
        createButton: function (button) {
            var me = this;

            switch (button.toLowerCase()) {

                case 'ok': return Wjs.new('Button')

                                .setText('Ok')

                                .onClick(function () {

                                    if (me.okHandler) {

                                        me.okHandler();
                                    }

                                    me.close();
                                });
                case 'cancel': return Wjs.new('Button')

                                .setText('Cancel')

                                .onClick(function () {

                                    if (me.cancelHandler) {

                                        me.cancelHandler();
                                    }

                                    me.close();
                                });

                default: throw 'Not implemented';
            }
        },

        setUseVisibility: function (flag) {

            this.getParent().setUseVisibility(flag);

            return this;
        },

        show: function () {

            this.getParent().show();

            return this;
        },

        hide: function () {

            this.getParent().hide();

            return this;
        },

        close: function (forceClosing) {

            if (this.beforeCloseHandler && !forceClosing) {

                if (this.beforeCloseHandler() === false) {

                    return; // Cancel closing the dialog
                }
            }

            this.hide();

            if (this.closeHandler) {

                this.closeHandler();
            }
        },

        destroy: function () {
            var overlay = this.getParent();

            // Detach the panel from the overlay so it won't try to destroy its item (single container)
            overlay.removeItem();

            this.getBase().destroy.call(this);

            overlay.destroy();
        }
    });

});